<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <xtx-bread>
        <xtx-bread-item to="/">首页</xtx-bread-item>
        <xtx-bread-item :to="`/cate/${goods.categories[1].id}`">{{ goods.categories[1].name }}</xtx-bread-item>
        <xtx-bread-item :to="`/cate/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}</xtx-bread-item>
        <xtx-bread-item to="/">{{ goods.name }}</xtx-bread-item>
      </xtx-bread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片组件 -->
        <div class="media">
          <!-- 图片预览组件 -->
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <!-- 销售信息 -->
          <GoodsSales :goods="goods"></GoodsSales>
        </div>
        <!-- 商品描述 -->
        <div class="spec">
          <!-- 商品信息 -->
          <GoodsName :goods="goods"></GoodsName>
          <!-- sku -->
          <GoodsSku :goods="goods" @change-sku="changeSkuHandler"></GoodsSku>
          <!-- 数量选择 -->
          <xtx-numbox label="数量" v-model="num" :max="goods.inventory"></xtx-numbox>
          <!--  加入购物车按钮  -->
          <xtx-button @click="insertCart" style="margin-top:20px;" type="primary">加入购物车</xtx-button>
        </div>
      </div>
      <!-- 商品推荐/猜你喜欢：商品推荐是根据商品id查询同类型的商品，猜你喜欢是根据用户的id去查找用户喜欢的商品 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
          <GoodWarn></GoodWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- 24小时热销榜 -->
          <GoodsHot :goodsId="goods.id" :type="1"></GoodsHot>
          <!-- 周热销榜 -->
          <GoodsHot :goodsId="goods.id" :type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { provide, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { getGoodsDetailApi } from '@/api/product'
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodWarn from './components/goods-warn.vue'
import Message from '@/components/library/message'
const route = useRoute()
const store = useStore()
// 获取商品详情数据
const goods = ref(null)
watch(() => route.params.id, async newVal => {
  if (newVal && route.path === `/product/${newVal}`) {
    const { result } = await getGoodsDetailApi(newVal)
    goods.value = result
    // console.log(result)
  }
}, { immediate: true })
// 提供goods数据给后代组件使用
provide('goods', goods)
// 获取sku组件传递的数据
// 定义变量保存当前选中的sku信息
const selectedSku = ref(null)
function changeSkuHandler(sku) {
  if (sku.skuId) {
    goods.value.price = sku.price
    goods.value.oldPrice = sku.oldPrice
    goods.value.inventory = sku.inventory
    selectedSku.value = sku
  } else {
    selectedSku.value = null
  }
}
// 该组合选择的数量
const num = ref(1)
// 加入购物车
async function insertCart() {
  // 判断用户是否勾选了sku组合
  if (selectedSku.value === null) {
    return Message({ text: '请选择规格' })
  }
  if (num.value > goods.value.inventory) {
    return Message({ text: '库存不足' })
  }
  // 准备添加购物车的数据
  const cartGoods = {
    id: goods.value.id,
    skuId: selectedSku.value.skuId,
    name: goods.value.name,
    picture: goods.value.mainPictures[0],
    price: goods.value.oldPrice,
    nowPrice: goods.value.price,
    count: num.value,
    attrsText: selectedSku.value.specsText,
    selected: true,
    isEffective: true,
    stock: selectedSku.value.inventory
  }
  // 提交vuex
  try {
    await store.dispatch('cart/insertCart', cartGoods)
    Message({ type: 'success', text: '添加购物车成功' })
  } catch (err) {
    Message({ type: 'error', text: '添加购物车失败' })
  }
}
</script>

<style lang="less" scoped>
// 商品信息
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background-color: #fff;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>